<?php

namespace App\Http\Controllers\Setting;

use App\Http\Controllers\Controller;
use App\Http\Requests\UserCreateRequest;
use App\Repositories\RoleRepositoryEloquent;
use Illuminate\Http\Request;
use App\Repositories\UserRepositoryEloquent;
use App\Http\Requests\UserUpdateRequest;
use DB, Breadcrumbs, Toastr;

class UserController extends Controller
{
    protected $user;
    protected $role;

    public function __construct(UserRepositoryEloquent $user, RoleRepositoryEloquent $role)
    {
        $this->user = $user;
        $this->role = $role;
    }

    public function index(Request $request)
    {
        $data['title'] = '用户列表';
        $data['data'] = DB::table('users')->select('*')->get();
        $data['users'] = $this->user->paginate(10);
        return view('setting.user.index', $data);
    }

    public function destroy($id)
    {
        $data['title'] = '用户禁用';
        $result = $this->user->delete($id);
        return response()->json(['ack' => $result]);
    }

    public function destroyAll(Request $request)
    {
        $data['title'] = '用户批量禁用';
        if(!($ids = $request->get('ids', []))) return response()->json(['ack' => 0, 'msg' => '请求参数错误']);
        foreach($ids as $id) $this->user->delete($id);
        return response()->json(['ack' => 1]);
    }

    public function create()
    {
        $data['title'] = '用户新增';
        return view('setting.user.create', $data);
    }

    public function store(UserCreateRequest $request)
    {
        $result = $this->user->store($request->all());
        if($result) return response()->json(['ack' => 1, 'msg' => '添加成功']);
        return response()->json(['ack' => 0, 'msg' => '请求参数错误']);
    }

    public function update(UserUpdateRequest $request, $id)
    {
        $result = $this->user->update($request->all(), $id);
        if(!$result['ack']) Toastr::error($result['msg']);
        Toastr::success('用户更新成功');
        return redirect(route('setting.user.edit', ['id' => $id]));
    }

    public function department()
    {
        $data['title'] = '部门管理';
    }

    public function edit($id)
    {
        $data['title'] = '用户修改';
        $data['user'] = $this->user->find($id);
        $data['roles'] = $this->role->all();
        $data['hasRoles'] = $data['user']->roles()->pluck('name')->toArray();
        return view('setting.user.edit', $data);
    }

    public function log(Request $request)
    {
        $data['title'] = '操作日志';
        $data['users'] = DB::table('users')->select('*')->where('status', 1)->get();
        $where = $page = [];
        if($request->input('start'))
        {
            $start = date('Y-m-d 00:00:00', strtotime($request->input('start')));
            $where[] = ['time', '>=', $start];
            $page['start'] = substr($start, 0 ,10);
        }
        if($request->input('end'))
        {
            $end = date('Y-m-d 23:59:59', strtotime($request->input('end')));
            $where[] = ['time', '<=', $end];
            $page['end'] = substr($end, 0 ,10);
        }
        if($request->input('user_id'))
        {
            $where[] = ['user_id', $request->input('user_id')];
            $page['user_id'] =  $request->input('user_id');
        }
        $data['logs'] = DB::table('users_log')->select('*')->where($where)->orderBy('id', 'desc')->paginate(20);
        $data['logs']->appends($page)->render();
        return view('setting.user.log', $data);
    }

}
